﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using userManagement;
using System.Web.UI.MobileControls;
using elementManagement;
using productsMarket;

namespace WebInterface
{

    public class InterfaceElement
    {
        string _Guid;
        public string Guid
        {
            get { return _Guid; }
            set { _Guid = value; }
        }
        string _Name;
        public string Name
        {
            get { return _Name; }
            set { _Name = value; }
        }
    }

    public class Accessors
    {

        public static User getCurrentUser()
        {
            userManagement.UsersDataPersister userManagement = UsersDataPersister.getInstance();
            string currentUserName = HttpContext.Current.User.Identity.Name;
            User currentUser = userManagement.getUser(currentUserName);

            return currentUser;
        }

        private List<InterfaceElement> getInterfaceElements(List<Element> elements)
        {
            List<InterfaceElement> interfaceElements = new List<InterfaceElement>();

            User currentUser = getCurrentUser();
            if (currentUser != null)
            {            

                foreach (Element el in elements)
                {
                    InterfaceElement elemInterf = new InterfaceElement();
                    elemInterf.Name = el.name + " (" + Convert.ToInt32(el.getCreditsValue()).ToString() + ")";
                    elemInterf.Guid = el.UniqueID;
                    interfaceElements.Add(elemInterf);
                }
            }
            return interfaceElements;
        }

        private List<InterfaceElement> getInterfaceElements(List<User> users)
        {
            List<InterfaceElement> interfaceElements = new List<InterfaceElement>();

            User currentUser = getCurrentUser();
            if (currentUser != null)
            {

                foreach (User u in users)
                {
                    InterfaceElement elemInterf = new InterfaceElement();
                    elemInterf.Name = u.name + " (" + u.getUserType().ToString() + ")";
                    elemInterf.Guid = u.name;
                    interfaceElements.Add(elemInterf);
                }
            }
            return interfaceElements;
        }

        public List<InterfaceElement> getCurrentUserInventory()
        {
            return getInterfaceElements(getCurrentUser().getInventory());
        }

        public List<InterfaceElement> getCurrentUserOnSale()
        {
            return getInterfaceElements(getCurrentUser().itemsForSale);
        }

        public List<InterfaceElement> getMarketProducts()
        {
            Market m = new Market();
            return getInterfaceElements(m.GetMarketProducts(getCurrentUser()));
        }

        public List<InterfaceElement> getCitizens()
        {
            return getInterfaceElements(UsersDataPersister.getInstance().userList);
        }

    }

   
}
